{include file="public/header-static"/}
{include file="public/sidebar"/}
<div id="main">

    {include file="public/header"/}

    <main class="main-content">

        <div class="container">
            {include file="public/page-header"/}
            <!-- end::page-header -->
            <div class="row">
                <div class="col-md-12">
                    <div class="card">
                        <div class="card-body">
                            <div class="app-block">
                                <div class="app-content">
                                    <div class="app-action">
                                        <div class="action-left">
                                            <ul class="list-inline">
                                                <li class="list-inline-item mb-0">
                                                    <a href="javascript:void(0);" onclick="add()" class="btn btn-secondary ">
                                                        <i data-feather="plus" class="mr-2"></i>
                                                        添加                                                                                                      </a>
                                                </li>

                                            </ul>
                                        </div>
                                        <div class="action-right">
                                            <a href="#" class="app-sidebar-menu-button btn btn-outline-light">
                                                <i data-feather="menu"></i>
                                            </a>
                                            <div class="input-group">
                                                <input type="text" name="keywords" id="keywords" autofocus="autofocus" value="{$Request.get.keywords}"  class="form-control" placeholder="搜索关键字[用户昵称|帖子描述]"
                                                       aria-describedby="button-addon1">
                                                <div class="input-group-append">
                                                    <button class="btn btn-outline-light" type="submit" id="button-addon1">
                                                        <i data-feather="search"></i>
                                                    </button>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                                <table id="linkstable" lay-filter="linkstable"></table>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </main>
    {include file="public/footer"/}
</div>
<style>
    #dis{background: #76CCFE;border-color: #76CCFE;color: #fff;}
</style>

<script type="text/html" id="countTpl">
    <span>评论：{{ d.comment }} </span><br>
    <span style="color:#23c6c8">点击：{{ d.click }} </span><br>
    <span>点赞：{{ d.like }} </span><br>
    <span style="color:#23c6c8">分享：{{ d.share }} </span>
</script>

<script type="text/html" id="switchTpl">
    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="显示|隐藏" lay-filter="switch_status" {{ d.status == 1 ? 'checked' : '' }}>
</script>

<script type="text/html" id="customTpl">
    {{# if(d.is_custom == 1) { }}
        <span>自定义</span>
    {{# } else { }}
        <span>正常</span>
    {{# } }}
</script>

<script type="text/html" id="barDemo">

    <button lay-event="see" class="btn btn-sm btn-outline-secondary">详情</button><br>
<!--    <button lay-event="edit" class="btn btn-sm btn-outline-secondary">编辑</button>-->
    <button lay-event="del" class="btn btn-sm btn-outline-secondary">删除</button>

</script>

<script type="text/html" id="toolbarDemo">
    <div class="count">

    </div>
</script>

<script src="__STATIC__/assets/js/custom.js"></script>

<script>
    function add() {
        window.location.href = '{:url("/console/postAdd")}';
    }
    function see(id) {
        window.location.href = '{:url("/console/postShow")}?id='+id;
    }
    function edit(id) {
        window.location.href = '{:url("/console/postEdit")}?id='+id;
    }
    function del(id) {
        $.post("{:url('/console/postDel')}", {id:id}, function(data) {
            if(data['code']!=1) {
                toastr.error(data['msg']);
            }else{
                window.location.reload();
            }
        });
    }

    // layui插件
    layui.use('table', function(){
        var table = layui.table
            ,form = layui.form;
        // 表格初始化
        table.render({
            elem: '#linkstable'
            ,url: '{:url("/console/post")}'
            ,method:'post'
            ,toolbar: '#toolbarDemo'
            ,defaultToolbar:[]
            ,page: true //开启分页
            ,cols: [[
                {field:'id', width:100, title:'帖子序号'}
                ,{field:'uid', width:100, title:'用户ID'}
                ,{field:'nickname', title:'用户昵称'}
                ,{field:'images', title:'封面', width:80, templet: function(d) {
                    if (d.images.length > 0) {
                        return '<div onclick="show_img(this)" ><img src="'+d.images+'" ' + 'alt="" width="60px" height="60px"></div>';
                    } else {
                        return '<div style="width:60px;height:60px"></div>';
                    }
                }}
                ,{field:'count', title:'统计', templet: '#countTpl', unresize: true}
                ,{field:'cate', title:'帖子分类'}
                ,{field:'is_custom', title:'帖子类型', templet: '#customTpl', unresize: true}
                ,{field:'content', title:'帖子描述'}
                ,{field:'create_time', title:'发帖时间'}
                ,{field:'rightbtn', title:'操作', toolbar: '#barDemo'}
            ]],
            done:function (res) {
                var element = $('td[data-field="rightbtn"]').children('.layui-table-cell');
                element.css('overflow','visible');
                element.children('.dropdown-menu').css('min-width','auto');
                // $('.count').append(
                //     '<span style="color: #666666;margin-right: 1.875rem">实时提交：<span style="color:#23c6c8">'+res.count+'</span></span><span style="color:#666666">实时待审核：<span style="color:#23c6c8">'+res.check_count+'</span></span>'
                // );
            },
            parseData: function(res){
                return {
                    "code": res.data.code,
                    "msg": res.data.msg,
                    "count": res.data.count,
                    // "check_count": res.data.check_count,
                    "data": res.data.list
                };
            }
        });
        table.on('tool(linkstable)', function(obj){
            var data = obj.data;
            // console.log(data)
            if (obj.event === 'see') {
                see(data.id);
            }
            if (obj.event === 'edit') {
                edit(data.id);
            }
            if(obj.event === 'del'){
                layer.confirm('删除该数据？', {icon: 3, title:'提示'}, function(index){
                    obj.del();
                    layer.close(index);
                    del(data.id);
                });
            }

        });

        // 搜索
        $('#button-addon1').on('click', function(){
            var keywords= $("#keywords").val();
            table.reload('linkstable', {
                url: '{:url("/console/post")}'
                ,where: {
                    keywords : keywords // 搜索参数
                }
                ,page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });

    });

    // 图片放大
    function show_img(element) {
        var elements = $(element).find("img");
        //页面层
        layer.open({
            type: 1,
            title: '图片',
            anim: 2,
            area: ['800px','500px'], //宽高 t.width() t.height()
            shadeClose: true, //开启遮罩关闭
            end: function (index, layero) {
                return false;
            },
            content: '<div style="text-align:center"><img src="' + $(elements).attr('src') + '" /></div>'
        });
    }
</script>